package com.fenghongzhang.day011;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.provider.Telephony;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * 1. 权限,
 *      1,1 静态->清单
 *      1.2 动态->代码
 * 2. 内容解析者,去访问手机联系人的数据库. -> 拿到数据toast
 * 3, 封装数据源
 * 4, 加载到listview
 * 5, 点击listview 拨打电话.
 */
public class MainActivity extends AppCompatActivity {

    private ListView lv;
    private List<Person> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.lv);


        //2,先版本判断
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]
                    {Manifest.permission.READ_CONTACTS,Manifest.permission.CALL_PHONE,Manifest.permission.READ_SMS
                    ,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}
                    ,120);
        }

        //lv的点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String number = list.get(position).getNumber();

                //拨打电话->操作一个intent
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:"+number));
                startActivity(intent);

            }
        });
    }

    //3
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 120 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this, "同意", Toast.LENGTH_SHORT).show();
        }else{
            finish();
        }
    }

    //4 一定要查看,自己的手机上,有没有联系人,如果没有,记得添加.
    public void click(View view) {

        ContentResolver contentResolver = getContentResolver();

        //改uri 手机联系人
//        Cursor query = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        //短信
//        Cursor query = contentResolver.query(Telephony.Sms.CONTENT_URI, null, null, null, null);
        //图片
//        Cursor query = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        //音频
        Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

        if(query != null){
            //数据源
            list = new ArrayList<>();

            while (query.moveToNext()){
                //联系人的字段
//                String name = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//                String number = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//                Toast.makeText(this, name+number, Toast.LENGTH_SHORT).show();
//                list.add(new Person(name,number));
                //短信的字段
//                String address = query.getString(query.getColumnIndex(Telephony.Sms.ADDRESS));
//                String body = query.getString(query.getColumnIndex(Telephony.Sms.BODY));
//                Toast.makeText(this, address+body, Toast.LENGTH_SHORT).show();

//                //图片的字段
//                String name = query.getString(query.getColumnIndex(MediaStore.Images.Media.TITLE));
//                String data = query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA));
//                Toast.makeText(this, name+data, Toast.LENGTH_SHORT).show();

                //音频的地段
                String name = query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String data = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));
                Toast.makeText(this, name+data, Toast.LENGTH_SHORT).show();

            }

            //适配器
            PersonAdapter personAdapter = new PersonAdapter(list, this);
            lv.setAdapter(personAdapter);

            query.close();
        }

    }
}
